﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class InstructorScheduleMainScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindScheduleInfo( );
            }
        }

        private void BindScheduleInfo()
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            dtgSchedule.DataSource = _currentCourse.Schedule;
            dtgSchedule.DataBind( );
        }

        protected void OnLnkBtnSeeTeamsWorkClicked(object sender, EventArgs e)
        {
            int _eventId = CommonMethods.GetIdFromGridViewRow((GridViewRow) (((LinkButton) sender).NamingContainer));
            Event _event = new Event(_eventId);

            //Event _event = CreateEventFromGridView((GridView) sender);
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            _currentCourse.GoToEventWork(_event);
        }

        //private Event CreateEventFromGridView(GridView sender)
        //{
        //    //TODO: Not Implemented
        //    return null;
        //}

        protected void OnLnkBtnExportToGoogleCalenderClicked(object sender, EventArgs e)
        {
            Response.Redirect("~/ExportToGoogleCalendarScreen.aspx");
        }
        
        protected void OnLnkBtnEditScheduleClicked(object sender, EventArgs e)
        {
            Response.Redirect( "~/InstructorEditScheduleScreen.aspx" );
        }

        protected void OnLnkBtnDownloadAllDocumentsClicked(object sender, EventArgs e)
        {
            Instructor _instructor = (Instructor) EntityFactory.User;
            _instructor.DownloadDocuments();
        }

        protected void OnLnkBtnDownloadSubmissionReportClicked(object sender, EventArgs e)
        {
            Instructor _instructor = (Instructor)EntityFactory.User;
            _instructor.DownloadSubmissionReport();
        }

        protected void OnDtgSchedulePreRender(object sender, EventArgs e)
        {
            foreach (GridViewRow _dataGridRow in dtgSchedule.Rows)
            {
                EventTableAdapter _eventTableAdapter = new EventTableAdapter();

                int _eventId;
                if (int.TryParse(((HiddenField)_dataGridRow.FindControl("hfId")).Value, out _eventId) == false)
                {
                    return;
                }

                bool _submissionExist = _eventTableAdapter.GetSubmissionExists(_eventId);

                if(_submissionExist == false)
                {
                    _dataGridRow.FindControl("lnkBtnSeeTeamsWork").Visible = false;
                }
            }
        }
    }
}